package com.jaya.budan.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.jaya.budan.R;
import com.jaya.budan.activity.GeRen_Update_Msg_PersonPhoto_Activity;
import com.jaya.budan.activity.PhotoAlbum_Activity;
import com.jaya.budan.activity.Square_Detail_Photo_Activity;
import com.jaya.budan.bean.ListUser_Information;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.CommonPopWindow;
import com.jaya.budan.views.NineGridView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.List;

public class NineGridViewAdapter implements Serializable {
    protected Context context;
    private List<ListUser_Information.Files> imageInfo;
    private String access_token;
    private int height = 0;
    public NineGridViewAdapter(Context context, List<ListUser_Information.Files> imageInfo, String token) {
        this.context = context;
        this.imageInfo = imageInfo;
        this.access_token = token;
    }

    /**
     * 如果要实现图片点击的逻辑，重写此方法即可
     *
     * @param context      上下文
     * @param nineGridView 九宫格控件
     * @param index        当前点击图片的的索引
     * @param imageInfo    图片地址的数据集合
     */
    public void onImageItemClick(Context context, NineGridView nineGridView, int index, List<ListUser_Information.Files> imageInfo){
        SharedPreferences preferences = context.getSharedPreferences("photo_path", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("path", imageInfo.get(index).getReal_path());
        editor.apply();
//        ToolUtils.showToast(context, "photo" + imageInfo.get(index).getReal_path());
        if (imageInfo.get(index).getSort().equals("1")){
            height = 550;
        } else {
            height = 600;
        }
        CommonPopWindow.newBuilder()
                .setView(R.layout.pop_geren_update_photo)
                .setAnimationStyle(R.style.AnimUp)
                .setBackgroundDrawable(new BitmapDrawable())
                .setSize(0, height)
                .setViewOnClickListener(new CommonPopWindow.ViewClickListener() {
                    @Override
                    public void getChildView(PopupWindow mPopupWindow, View view, int mLayoutResId) {
                        TextView tv_photo_1 = view.findViewById(R.id.tv_photo_1);
                        TextView tv_photo_2 = view.findViewById(R.id.tv_photo_2);
                        TextView tv_photo_3 = view.findViewById(R.id.tv_photo_3);
                        TextView tv_photo_4 = view.findViewById(R.id.tv_photo_4);
                        TextView tv_pop_diandian_cancle = view.findViewById(R.id.tv_pop_diandian_cancle);
                        if (imageInfo.get(index).getSort().equals("1")){
                            tv_photo_3.setVisibility(View.GONE);
                        } else {
                            tv_photo_3.setVisibility(View.VISIBLE);
                        }
                        tv_photo_1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mPopupWindow.dismiss();
                                //图片放大
                                Intent intent = new Intent(context, Square_Detail_Photo_Activity.class);
                                intent.putExtra("access_token", access_token);
                                intent.putExtra("roomId", index);
                                intent.putExtra("information", (Serializable) imageInfo);
                                context.startActivity(intent);
                            }
                        });
                        tv_photo_2.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mPopupWindow.dismiss();//添加图片
                                Intent intent6 = new Intent(context, PhotoAlbum_Activity.class);
                                intent6.putExtra("access_token", access_token);
                                intent6.putExtra("bucket", "photo");
                                intent6.putExtra("cos_type", 0);
                                context.startActivity(intent6);
                            }
                        });
                        tv_photo_3.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mPopupWindow.dismiss();
                                JSONObject object = new JSONObject();
                                try {
                                    object.put("p_id", imageInfo.get(index).getId());
                                    object.put("type", "2");// 1删除 2设置大图
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                postData(object, "2");
                            }
                        });
                        tv_photo_4.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mPopupWindow.dismiss();
                                JSONObject object = new JSONObject();
                                try {
                                    object.put("p_id", imageInfo.get(index).getId());
                                    object.put("type", "1");// 1删除 2设置大图
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                postData(object, "1");
                            }
                        });
                        tv_pop_diandian_cancle.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mPopupWindow.dismiss();
                            }
                        });
                    }
                })
                .setBackgroundDarkEnable(true)
                .setBackgroundAlpha(0.7f)
                .build(context)
                .showAsBottom(nineGridView);
    }

    /**
     * 生成ImageView容器的方式，
     * 如果需要自定义图片展示效果，重写此方法即可
     *
     * @param context 上下文
     * @return 生成的 ImageView
     */
    public ImageView generateImageView(Context context) {
        ImageView imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//        imageView.setImageResource(R.mipmap.banner);
        return imageView;
    }

    public List<ListUser_Information.Files> getImageInfo() {
        return imageInfo;
    }

    public void setImageInfoList(List<ListUser_Information.Files> imageInfo) {
        this.imageInfo = imageInfo;
    }

    /**
     * 网络请求
     */
    @SuppressLint("SetTextI18n")
    private void postData(JSONObject object, String type) {
        Log.d("square", object.toString());
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, RequestConnect.tr_user_information_photo_update_Url + access_token, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("square", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        if (type.equals("1")){
                            GeRen_Update_Msg_PersonPhoto_Activity.activity.finish();
//                            GeRen_Update_Msg_PersonPhoto_Activity.activity.recreate();
                        } else {
                            ToolUtils.showToast(context, message);//设置成功
                        }
                    } else {
                        ToolUtils.showToast(context, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(context, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(context).add(request);
    }
}
